<template>
	<view class="bg-co-g wdh-100 columnbox content jus">
		<view v-for="(item,index) in list" :key='index' class="coupon-one rowbox spb als">
			<view class="bgcoupon">
				<image class="img" src="../../static/img/couponBlue.png" mode=""></image>
			</view>
			<view class="columnbox left-box">
				<view class="coupon-num-box">{{item.price}} <text>元</text> </view>
				<view class="coupon-rule">满{{item.fullprice}}元使用</view>
			</view>
			<view class="right-box columnbox als">
				<view class="rowbox wdh-100 spb">
					<view class="coupon-title">{{item.name}}</view>
					<!-- <view class="rowbox use-btn" @click="editCoupon(item.id)">编辑</view> -->
				</view>
				<view class="numcont">
					<view class="item">
						<view class="label">
							查看次数：
						</view>
						<view class="val">
							{{item.seenum}}
						</view>
					</view>
					<view class="item">
						<view class="label">
							领取次数：
						</view>
						<view class="val">
							{{item.lqnum}}
						</view>
					</view>
				</view>
				<!-- <view class="wdh-100 coupon-date">有效期至{{item.endtime}}</view> -->
				<view class="wdh-100 coupon-date rowbox jus">
					<view class="rowbox use-btn" v-if="item.status==1" @click="couponOffline(item)">下架</view>
					<view class="rowbox use-btn" v-if="item.status==2" @click="couponOnline(item)">上架</view>
					<view class="rowbox use-btn" @click="handleGorecive(item)">领取记录</view>
					<view class="rowbox use-btn" @click="handleUse(item)">使用记录</view>
				</view>
			</view>
		</view>
		<view class="footerbtncont">
			<view class="weekNotice" v-if="showWeekNotice">
				<view class="">
					你的试用额度即将用完
				</view>
				<view class="btncont">
					<u-button v-if="!isIos" type="primary" size="mini" text="续费" @click="handleShowAndrRenw"></u-button>
					<u-button v-if="isIos" type="primary" size="mini" text="联系客服" @click="handleKefu"></u-button>
				</view>
			</view>
			<view @click="addCoupon" class="rowbox def-bgcol add-btn">+添加</view>
			<view @click="handlezhuanfa" class="rowbox def-bgcol add-btn zfbtn">分享</view>
		</view>
		<IosKefu :show="showIosKefu" :type="matype" @close="closeIosKefu"></IosKefu>
		<AndrTaocan :show="showAndrTaocan" :taocanid="3" @close="closeAndrTaocan" @refsh="handlerefsh"></AndrTaocan>
	</view>
</template>

<script>
	import dayjs from 'dayjs'
	import getdate from '@/utils/time.js'
	import IosKefu from '@/components/ioskefu.vue'
	import AndrTaocan from '@/components/andrtaocan.vue'
	export default {
		components: {
			IosKefu,
			AndrTaocan
		},
		data() {
			return {
				showAndrTaocan: false,

				matype: '',
				showIosKefu: false,
				isIos: false,
				showWeekNotice: false,
				storeInfo: '',
				greenBgImg: '/static/img/couponGreen.png',
				blueBgImg: '/static/img/couponBlue.png',
				list: [],
				id: 0,
				refreshflag: false
			};
		},
		onLoad(options) {
			this.id = parseInt(options.id)
			// #ifdef MP-WEIXIN
			let base64PngBlue = wx.getFileSystemManager().readFileSync("/static/img/couponBlue.png", 'base64');
			this.greenBgImg = 'data:image/jpg;base64,' + base64PngBlue
			// #endif
			this.getInfo()
		},
		computed: {

		},
		onShow() {
			const info = wx.getSystemInfoSync()
			this.isIos = !(info.system.indexOf('Android') > -1)

			if (this.refreshflag) {
				this.refreshflag = false
				this.list = []
				this.getInfo()
			}
			this.getStrorInfo()
		},

		methods: {
			handlerefsh() {
				this.getStrorInfo()
			},
			handleShowAndrRenw() {
				this.showAndrTaocan = true
			},
			closeAndrTaocan() {
				this.showAndrTaocan = false
			},
			handleKefu() {
				this.showIosKefu = true
				this.matype = 'yhjimg'
			},
			closeIosKefu() {
				this.showIosKefu = false
			},
			getStrorInfo() {
				this.$req.post('/shanghu/shanghuuserInfo', {
					uid: uni.getStorageSync('uid')
				}).then((res) => {
					let data = res.data.data
					if (data.yhjnum < 10) {
						this.showWeekNotice = true
					}
					if (data.yhjnum < 2) {
						let flag = true
						if (uni.getStorageSync('yhjtaoendtime')) {
							var ctime = new Date().getTime()
							if (ctime > uni.getStorageSync('yhjtaoendtime')) {
								uni.removeStorageSync('yhjtaoendtime')
							} else {
								flag = false
							}
						}
						if (this.isIos) {
							this.matype = 'yhjimg'
							this.showIosKefu = flag
						} else {
							this.showAndrTaocan = flag
						}

						if (!uni.getStorageSync('yhjtaoendtime')) {
							let endTime = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 *
								60 *
								1000 - 1); // 当天23:59
							uni.setStorageSync('yhjtaoendtime', endTime)
						}
					}

				})
			},
			handlezhuanfa() {
				uni.navigateToMiniProgram({
					appId: 'wx42f52aeb0203685a',
					path: `/pages/couponCenter/couponCenter?shanghuid=${uni.getStorageSync('userInfo').shanghuid}`
				})
			},
			handleGorecive(data) {
				uni.navigateTo({
					url: '/pages/couponreceivelist/couponreceivelist?id=' + data.id
				})
			},
			handleUse(data) {
				uni.navigateTo({
					url: '/pages/couponuselist/couponuselist?id=' + data.id
				})
			},
			editCoupon(id) {
				uni.navigateTo({
					url: './add?id=' + id
				})
			},
			couponOffline(val) {
				this.$req.post('/shanghu/couponupdate', {
					id: val.id,
					status: 2
				}).then(res => {
					val.status = 2
				})
			},
			couponOnline(val) {
				this.$req.post('/shanghu/couponupdate', {
					id: val.id,
					status: 1
				}).then(res => {
					val.status = 1
				})
			},
			getInfo() {
				this.$req.post('/shanghu/coupons', {
					shanghuid: uni.getStorageSync('userInfo').shanghuid
				}).then(res => {
					res.data.data.forEach(resp => {
						resp.endtime = getdate.couponDate(resp.endtime)
					})
					this.list = res.data.data
				})
			},
			addCoupon() {
				uni.navigateTo({
					url: '/pages/coupon/add'
				})
			}
		}
	}
</script>

<style lang="scss">
	.weekNotice {
		padding: 15rpx 30rpx;
		position: fixed;
		bottom: 240rpx;
		left: 0;
		z-index: 100;
		width: 100%;
		background-color: #fdf6ec;
		color: #f9ae3d;
		font-size: 30rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.footerbtncont {
		position: fixed;
		width: 100%;
		height: 240rpx;
		padding: 10rpx 24rpx 0 24rpx;
		border-top: 1px solid #edf0f2;
		left: 0;
		bottom: 0;
		z-index: 5;

		.zfbtn {
			margin-top: 20rpx;
		}
	}

	.numcont {
		display: flex;
		padding-top: 20rpx;

		.item {
			display: flex;
			margin-right: 30rpx;
			font-size: 26rpx;

			.label {
				color: #999;
			}
		}
	}

	.content {
		min-height: 100vh;
		padding-bottom: 320rpx;
	}

	.coupon-one {
		width: 702rpx;
		height: 220rpx;
		background-size: 100% 100%;
		background-repeat: no-repeat;
		margin-top: 20rpx;
		position: relative;

		.bgcoupon {
			position: absolute;
			left: 0;
			top: 0;
			width: 100%;
			height: 100%;

			.img {
				display: block;
				width: 100%;
				height: 100%;
			}
		}

		.left-box {
			height: 100%;
			width: 186rpx;
			position: relative;
			z-index: 2;

			.coupon-num-box {
				color: #fff;
				font-size: 70rpx;
				font-family: D-DIN Exp-DINExp, D-DIN Exp-DINExp;
				font-weight: bold;
				color: #FFFFFF;
				line-height: 70rpx;

				text {
					font-size: 22rpx;
					font-family: Source Han Sans CN, Source Han Sans CN;
					font-weight: bold;
					color: #FFFFFF;
					line-height: 26rpx;
				}
			}

			.coupon-rule {
				margin-top: 20rpx;
				font-size: 20rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 23rpx;
			}
		}

		.right-box {
			padding-left: 36rpx;
			width: calc(100% - 186rpx);
			margin-top: 32rpx;
			position: relative;
			z-index: 2;

			.coupon-title {
				font-size: 30rpx;
				font-family: Source Han Sans CN, Source Han Sans CN;
				font-weight: bold;
				color: #202020;
				line-height: 35rpx;
			}

			.use-btn {
				font-size: 24rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				color: rgba(13, 90, 211, 0.77);
				line-height: 28rpx;
				height: 46rpx;
				border-radius: 40rpx;
				border: 2rpx solid #B1CDF9;
				margin-right: 36rpx;
				padding: 0 20rpx;
			}

			.coupon-notice {
				font-size: 20rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				color: #7B838F;
				line-height: 23rpx;
				margin-top: 10rpx;
			}

			.coupon-date {
				font-size: 20rpx;
				font-family: Source Han Sans CN, Source Han Sans CN;
				font-weight: 400;
				color: #6D798A;
				line-height: 23rpx;
				margin-top: 20rpx;
			}
		}
	}

	.add-btn {
		color: #fff;
		width: 100%;
		height: 80rpx;
		border-radius: 100rpx;

	}
</style>